/*
 * Copyright 2011 The Rusted Roof, LLC.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.proteusframework.platformservice.messaging;

import org.proteusframework.core.api.model.INamespace;
import org.proteusframework.core.base.AbstractObject;
import org.proteusframework.core.base.DisplayableName;
import org.proteusframework.platformservice.AbstractServicePlugin;

import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Base class for all <code>IMessageService</code> implementations.
 *
 * @author Tacoma Four
 */
public abstract class AbstractMessageService extends AbstractServicePlugin implements IMessageService
{
    /**
     * Lock used to provide thread-safe access to the {@link #requestMessageSink(org.proteusframework.core.api.model.INamespace, Class)} and
     * {@link #requestMessageSink(org.proteusframework.core.api.model.INamespace, Class, java.util.Properties)} methods.
     */
    protected final Object lock = new Object();

    /**
     * Concurrent HashMap used to manage the map of namespace-message sink instances.
     */
    private Map<INamespace, Object> registeredMessageSinkMap = new ConcurrentHashMap<INamespace, Object>();

    protected AbstractMessageService(INamespace namespace)
    {
        super(new DisplayableName(namespace), AbstractMessageService.class);
    }

    protected final <T extends IMessageBean> void putSink(INamespace namespace, IMessageSink<T> messageSink)
    {
        registeredMessageSinkMap.put(namespace, messageSink);
    }

    @SuppressWarnings("unchecked")
    protected final <T extends IMessageBean> IMessageSink<T> getSink(INamespace namespace)
    {
        return (IMessageSink<T>) registeredMessageSinkMap.get(namespace);
    }

    protected final boolean sinkExists(INamespace namespace)
    {
        return registeredMessageSinkMap.containsKey(namespace);
    }

    /**
     * Convenience method that invokes {@link #requestMessageSink(INamespace, Class, java.util.Properties)}, passing an
     * empty non-null <code>Properties</code> object.
     *
     * @param namespace        Namespace
     * @param messageBeanClass Message bean class
     * @param <T>              Specific type of message bean that the message sink can handle.
     * @return Message sink instance
     * @throws IllegalArgumentException when the message sink already exists, but there is a mis-match between the
     *                                  namespace and messageBeanClass.
     */
    @Override
    public <T extends IMessageBean> IMessageSink<T> requestMessageSink(INamespace namespace, Class<T> messageBeanClass) throws IllegalArgumentException
    {
        return requestMessageSink(namespace, messageBeanClass, new Properties());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public <T extends IMessageBean> boolean exists(INamespace namespace, Class<T> messageBeanClass)
    {
        boolean exists;
        if (registeredMessageSinkMap.containsKey(namespace))
        {
            IMessageSink<T> sink = getSink(namespace);
            exists = (sink.getMessageBeanClass() == messageBeanClass);
        } else
        {
            exists = true;
        }

        return exists;
    }
}
